package com.rongxue.controller.index;



import com.rongxue.mapper.SAddressMapper;
import com.rongxue.mapper.SOrderMapper;
import com.rongxue.mapper.SProductMapper;
import com.rongxue.pojo.*;
import com.rongxue.service.Impl.CashInAndOutServiceImpl;
import com.rongxue.service.ProductService;
import com.rongxue.utils.CommonUtils;
import com.rongxue.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by yangweijian on 2018/2/27.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private SOrderMapper sOrderMapper;

    @Autowired
    private SProductMapper sProductMapper;

    @Autowired
    private SAddressMapper sAddressMapper;

    /**
     * 积分商城
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/index")
    @ResponseBody
    public ObjectResult index(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = productService.index(xmlBean);
        }catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }

    /**
     * 商品详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/details")
    @ResponseBody
    public ObjectResult details(XMLBean xmlBean){
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = productService.details(xmlBean);
        }catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }


    /**
     * 兑换商品
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/exchange")
    @ResponseBody
    public ObjectResult exchange(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = productService.exchange(xmlBean);
        }catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","兑换失败");
        }
        return objectResult;
    }

    /**
     * 添加收货地址
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/add_address")
    @ResponseBody
    public ObjectResult add_address(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = productService.insertAddress(xmlBean);
        }catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }

    /**
     * 收货地址列表
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/address_list")
    @ResponseBody
    public ObjectResult address_list(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        Map<String,Object> map = new HashMap<>();
        String userId = CommonUtils.decToken(xmlBean.getToken());
        map.put("userId",userId);
        List<SAddress> list = sAddressMapper.selectAddressByUserId(map);
        return ObjectResult.build("1","成功",list);
    }

    /**
     * 修改收获地址
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/edit_address")
    @ResponseBody
    public ObjectResult edit_address(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = productService.edit_address(xmlBean);
        }catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }

    /**
     * 删除收获地址
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/del_address")
    @ResponseBody
    public ObjectResult del_address(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = productService.del_address(xmlBean);
        }catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }

    /**
     * 订单
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/exchange_record")
    @ResponseBody
    public ObjectResult exchange_record(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("status",xmlBean.getStatus());
        List<SOrder> orders = sOrderMapper.selectexchangeListByUseerId(map);
        Page<SOrder> page = new Page<>();
        page.setP(xmlBean.getP()==null? 1:xmlBean.getP());
        page.setPageSize(10d);
        List<SOrder> content = page.getPageContent(orders);
        for(SOrder sOrder:content){
            sOrder.setImage(CashInAndOutServiceImpl.ADMIN_URL+sOrder.getImage());
        }

        map.clear();
        map.put("list",orders);
        map.put("count",page.getPageTotal());
        return ObjectResult.build("1","成功",orders);
    }

    /**
     * 订单详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/exchange_detail")
    @ResponseBody
    public ObjectResult exchange_detail(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        String userId = CommonUtils.decToken(xmlBean.getToken());

        SOrder order = sOrderMapper.selectOrderById(Integer.parseInt(xmlBean.getId()));
        order.setCustomer_mobile("17319350351");
        order.setImage(CashInAndOutServiceImpl.ADMIN_URL+order.getImage());

        return ObjectResult.build("1","成功",order);
    }

    /**
     * 修改订单状态
     * @param xmlBean
     * @return
     */
        @RequestMapping(value = "/updateStatus")
    @ResponseBody
    public ObjectResult updateStatus(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        Map<String,Object> map = new HashMap<>();
        map.put("status",xmlBean.getStatus());
        map.put("id",xmlBean.getId());
        sOrderMapper.updateStatusByid(map);
        return ObjectResult.build("1","成功");
    }
}
